package com.feishi.core.media.javacv;

import org.bytedeco.javacpp.opencv_core.*;

import java.util.*;

import static org.bytedeco.javacpp.opencv_imgcodecs.*;
import static org.bytedeco.javacpp.opencv_imgproc.*;

/**
 * @version 1.0
 * @auth caixq
 * @time 18-6-1 15:32
 **/
public class Contours {
    public static void main(String[] args) {
        Mat threshImg=imread("/home/caixq/Desktop/testA4.png");

        Mat hierarchy = new Mat();
        Mat gray=new Mat();
        cvtColor(threshImg, gray, COLOR_BGR2GRAY);
        MatVector contours=new MatVector();
        findContours(gray,contours,hierarchy,RETR_EXTERNAL,CHAIN_APPROX_SIMPLE);
        //计算轮廓距
        List<Moments> momList = new ArrayList();
        Moments momentsItem;
        Map<String, Integer> shapeResult = new HashMap<String, Integer>();
        for( int i = 0; i < contours.size(); i++ ){
            Mat o=contours.get(i);
            String shape;
            String color;
//            momentsItem =  Imgproc.moments(contours.get(i), false);
//            int cx = (int) ((int) (momentsItem.get_m10() / momentsItem.get_m00()) *ratio) ;
//            int cy = (int) ((int) (momentsItem.get_m01() / momentsItem.get_m00()) *ratio);
            double peri = arcLength(o, true);
            Mat o1=new Mat();
            approxPolyDP(o,o1,0.04*peri,true);

            System.out.println(peri);



        }

    }
}
